package com.raja.servingStation.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.raja.servingStation.dao.OrderDetailDao;
import com.raja.servingStation.entity.FirstInsuranceOrder;
import com.raja.servingStation.entity.RepairOrderDetail;
import com.raja.servingStation.entity.RepairPlan;
import com.raja.servingStation.entity.RepairResult;
import com.raja.servingStation.service.OrderDetailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zhangzhen on 2017/10/10.
 */
@Service
public class OrderDetailServiceImpl implements OrderDetailService {
    @Autowired
    private OrderDetailDao orderDetailDao;


    @Override
    public RepairOrderDetail getDetailByOrderId(JSONObject body) throws Exception {
        RepairOrderDetail repairOrderDetail = orderDetailDao.getDetailByOrderId(body);
        String orderId = body.getString("orderId");
        if (null == repairOrderDetail) {
            return null;
        }
        // 故障诊断
        repairOrderDetail.setFaultDiacrisisParts(orderDetailDao.getGzzd(orderId));
        // 维修方案
        List<RepairPlan> repairPlans = orderDetailDao.getWxfa(orderId);
        // 维修结果
        List<RepairResult> repairResults = orderDetailDao.getWxjg(orderId);
        List<RepairPlan> _repairPlans = new ArrayList<RepairPlan>();
        for (int i = 0; i < repairPlans.size(); i++) {
            RepairPlan repairPlan = repairPlans.get(i);
            List<RepairResult> _repairResults = new ArrayList<RepairResult>();

            if("1".equals(repairPlan.getPriceType())) { // 1.部件费用
                for (RepairResult repairResult : repairResults) {
                    if (repairPlan.getPpId().toString().equals(repairResult.getPlanId())) {
                        _repairResults.add(repairResult);
                    }
                }
            }
            repairPlan.setRepairResult(_repairResults);
            _repairPlans.add(repairPlan);
        }
        repairOrderDetail.setRepairPlans(_repairPlans);

        // 工时费
        repairOrderDetail.setTbLaborCosts(orderDetailDao.getWxfagsf(orderId));

        // 维修图片
        repairOrderDetail.setOrderPics(orderDetailDao.getOrderPic(orderId));
        return repairOrderDetail;
    }

    @Override
    public FirstInsuranceOrder getSbDetailByOrderId(JSONObject body) throws Exception {
        return orderDetailDao.getSbDetailByOrderId(body);
    }

    @Override
    public Object editOrderByOrderId(JSONObject body) throws Exception {
        return orderDetailDao.editOrderByOrderId(body);
    }
}
